package com.fastwire.carriere.callable;

import java.util.List;
import java.util.concurrent.Callable;

import com.fastwire.carriere.common.constant.AppConstants;
import com.fastwire.carriere.common.exception.ResourceException;
import com.fastwire.carriere.common.util.CommonLogger;
import com.fastwire.carriere.dao.DAOException;
import com.fastwire.carriere.discovery.NetworkEntityConcurrentDiscovery;
import com.fastwire.carriere.inventory.entity.CrossConnect;
import com.fastwire.carriere.inventory.entity.FDFr;
import com.fastwire.carriere.inventory.entity.NetworkElement;
import com.fastwire.carriere.inventory.entity.ProtectionGroup;
import com.fastwire.carriere.inventory.entity.TerminationPoint;
import com.google.common.collect.Lists;

public class ConcurrentDiscoveryInteraction implements Callable<List> {

	private List<NetworkElement> neList;
	private final String type;

	public ConcurrentDiscoveryInteraction(List<NetworkElement> neList,
			String type) {
		super();
		this.neList = neList;
		this.type = type;
	}

	public void setNeList(List<NetworkElement> neList) {
		this.neList = neList;
	}

	@Override
	public List call() throws DAOException, ResourceException {
		NetworkEntityConcurrentDiscovery neDiscovery = new NetworkEntityConcurrentDiscovery();

		List entityList = Lists.newArrayList();

		List<FDFr> fdFrs = Lists.newArrayList();
		List<CrossConnect> crossconnects = Lists.newArrayList();
		List<TerminationPoint> tps = Lists.newArrayList();
		List<ProtectionGroup> pgs = Lists.newArrayList();

		try {

			List<NetworkElement> validNetworkElements = Lists
					.newArrayList(neList);

			int index = 0;

			for (NetworkElement ne : neList) {
				try {

					fdFrs.addAll(neDiscovery.discoverAllFDFrbyNE(ne.getName()));

					crossconnects.addAll(neDiscovery
							.discoverAllCrossConnectsbyNE(ne));
					if (AppConstants.DISCOVERY_TYPE_FULL.equals(type)
							|| AppConstants.DISCOVERY_TYPE_ADHOC.equals(type)) {
						tps.addAll(neDiscovery.discoverAllTPsbyNE(ne.getName()));
					}

					pgs.addAll(neDiscovery.discoverAllProtectionGroupsbyNE(ne
							.getName()));

					CommonLogger.info(this, "done service: " + index++);
				} catch (Exception e) {
					e.printStackTrace();
					CommonLogger.error(this, "ne name:" + ne.getName()
							+ ", error during discovery:" + e.getMessage());
					// to remove any NEs that might be uninstalled /invalidated
					// on the field
					validNetworkElements.remove(ne);
				}
			}

			if (AppConstants.DISCOVERY_TYPE_FULL.equals(type)
					|| AppConstants.DISCOVERY_TYPE_ADHOC.equals(type)) {

				index = 0;
				for (NetworkElement ne : validNetworkElements) {

					try {
						ne.setEquipments(neDiscovery
								.discoverAllEquipmentsbyNE(ne));

						CommonLogger.info(this, "done equipment: " + index++);

					} catch (Exception e) {
						e.printStackTrace();
						CommonLogger.error(this, "ne name:" + ne.getName()
								+ ", error msg:" + e.getMessage());

					}
				}
			}

			if (AppConstants.DISCOVERY_TYPE_FULL.equals(type)
					|| AppConstants.DISCOVERY_TYPE_ADHOC.equals(type)) {
				entityList.addAll(validNetworkElements);
			}
			entityList.addAll(fdFrs);
			entityList.addAll(crossconnects);
			entityList.addAll(pgs);
			if (AppConstants.DISCOVERY_TYPE_FULL.equals(type)
					|| AppConstants.DISCOVERY_TYPE_ADHOC.equals(type)) {
				entityList.addAll(tps);
			}

		} catch (Exception e) {

			CommonLogger.info(this, "ExceptionException" + e.getMessage());
			e.printStackTrace();

		}

		return entityList;

	}

}
